using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class BaoCaoChiTietThuocDAO
    {
        public static bool Insert(BaoCaoChiTietThuocDTO bc)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaChiTietBaoCaoThuoc", bc.MaChiTietBaoCaoThuoc));
                sqlparam.Add(new SqlParameter("@MaBaoCaoThuoc", bc.MaBaoCaoThuoc));
                sqlparam.Add(new SqlParameter("@MaThuoc", bc.MaThuoc));
                sqlparam.Add(new SqlParameter("@MaDonVi", bc.MaDonVi));
                sqlparam.Add(new SqlParameter("@SoLuongThuoc", bc.SoLuongThuoc));
                sqlparam.Add(new SqlParameter("@SoLanDungThuoc", bc.SoLanDungThuoc));               

                int n = SqlDataAccessHelper.ExecuteNoneQuery("spBaoCaoChiTietThuocInsert", sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Delete(BaoCaoChiTietThuocDTO bc)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaChiTietBaoCaoThuoc", bc.MaChiTietBaoCaoThuoc));
                

                int n = SqlDataAccessHelper.ExecuteNoneQuery("spBaoCaoChiTietThuocDelete", sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Update(BaoCaoChiTietThuocDTO bc)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@MaChiTietBaoCaoThuoc", bc.MaChiTietBaoCaoThuoc));
                sqlparam.Add(new SqlParameter("@MaBaoCaoThuoc", bc.MaBaoCaoThuoc));
                sqlparam.Add(new SqlParameter("@MaDonVi", bc.MaDonVi));
                sqlparam.Add(new SqlParameter("@MaThuoc", bc.MaThuoc));
                sqlparam.Add(new SqlParameter("@SoLanDungThuoc", bc.SoLanDungThuoc));
                sqlparam.Add(new SqlParameter("@SoLuongThuoc", bc.SoLuongThuoc));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("spBaoCaoChiTietThuocUpdate", sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static List<BaoCaoChiTietThuocDTO> Select()
        {
            List<BaoCaoChiTietThuocDTO> lst = new List<BaoCaoChiTietThuocDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spBaoCaoChiTietThuocSelect");
                foreach (DataRow dr in dt.Rows)
                {
                    BaoCaoChiTietThuocDTO bc = new BaoCaoChiTietThuocDTO();
                    bc.MaChiTietBaoCaoThuoc = int.Parse(dr["MaChiTietBaoCaoThuoc"].ToString());
                    bc.MaBaoCaoThuoc = int.Parse(dr["MaBaoCaoThuoc"].ToString());
                    bc.MaDonVi = int.Parse(dr["MaDonVi"].ToString());
                    bc.MaThuoc = int.Parse(dr["MaThuoc"].ToString());
                    bc.SoLanDungThuoc = int.Parse(dr["SoLanDungThuoc"].ToString());
                    bc.SoLuongThuoc = int.Parse(dr["SoLuongThuoc"].ToString());
                    lst.Add(bc);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lst;
        }
        public static BaoCaoChiTietThuocDTO SelectByID(int MaChiTietBaoCaoThuoc)
        {
            BaoCaoChiTietThuocDTO bc = new BaoCaoChiTietThuocDTO();
            try
            {
                List<SqlParameter> sqlParam = new List<SqlParameter>();
                sqlParam.Add(new SqlParameter("@MaChiTietBaoCaoThuoc",MaChiTietBaoCaoThuoc));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spBaoCaoChiTietThuocSelectByID",sqlParam);
                DataRow dr = dt.Rows[0];
                bc.MaChiTietBaoCaoThuoc = int.Parse(dr["MaChiTietBaoCaoThuoc"].ToString());
                bc.MaBaoCaoThuoc = int.Parse(dr["MaBaoCaoThuoc"].ToString());
                bc.MaDonVi = int.Parse(dr["MaDonVi"].ToString());
                bc.MaThuoc = int.Parse(dr["MaThuoc"].ToString());
                bc.SoLanDungThuoc = int.Parse(dr["SoLanDungThuoc"].ToString());
                bc.SoLuongThuoc = int.Parse(dr["SoLuongThuoc"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return bc;
        }
    }
}
